<?php

namespace app\shop\controller\sys_admin;

use think\Db;

use app\AdminController;
use app\shop\model\GoodsModel;
use app\shop\model\ShippingTplModel;
use app\shop\model\PrizeSetModel;



/**
 * 商品相关
 * Class Index
 * @package app\store\controller
 */
class PrizeSet extends AdminController
{
    public $ext_status = 0;//额外默认指定商品状态
    public $is_supplyer = false;//是否后台供应商管理
    public $supplyer_id = 0;//当前操作的供应商ID
    public $_field = '';
    public $_pagesize = 0;
    //*------------------------------------------------------ */
    //-- 初始化
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new PrizeSetModel();
        $this->assign('is_supplyer', $this->is_supplyer);
    }

    //*------------------------------------------------------ */
    //-- 首页
    /*------------------------------------------------------ */
    public function index()
    {
        $this->getList(true);
        return $this->fetch('index');
    }
    /*------------------------------------------------------ */
    //-- 获取列表 奖品列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false, $is_delete = 0)
    {
        $this->assign('listType', $this->action);
        $runJson = input('runJson', 0, 'intval');

        $search['keyword'] = input('keyword', '', 'trim');
        if (empty($search['keyword']) == false) {
            $where['and'][] = "( goods_name like '%" . $search['keyword'] . "%')  OR ( goods_sn like '%" . $search['keyword'] . "%')";
        }

        $where[] = ['is_delete', '=', $is_delete];

        $data = $this->getPageList($this->Model, $where, $this->_field, $this->_pagesize);
        //dump($data);

        $this->assign("data", $data);
        $this->assign("search", $search);


        if ($runJson == 1) {
            return $this->success('', '', $data);
        } elseif ($runData == false) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('', '', $data);
        }
        return true;
    }

    /*------------------------------------------------------ */
    //-- 信息页调用
    //-- $data array 自动读取对应的数据
    /*------------------------------------------------------ */
    public function asInfo($data)
    {
        $this->Model = new PrizeSet();


        $ShippingTplModel = new ShippingTplModel();
        if (settings('shipping_tmp_supplyer') == 0) {
            $this->assign("ShippingTpl", $ShippingTplModel->getRows());//运费模板
        } elseif ($this->is_supplyer == true) {
            $this->assign("ShippingTpl", $ShippingTplModel->getRows($data['supplyer_id']));//运费模板
        } else {
            $this->assign("ShippingTpl", $ShippingTplModel->getRows($this->supplyer_id));//运费模板
        }



        //$data['paylist']
        //dump(config('config.withdraw_account_type'));die;
        //dump($payTypeList);die;

        // $data['pay_type'] =explode(',',$data['pay_type']);

        //dump(strstr($data['pay_type'],'alipay'));die;
        return $data;
    }



    /*------------------------------------------------------ */
    //-- 添加前调用
    /*------------------------------------------------------ */
    public function beforeAdd($row)
    {

        //$row = $this->checkData($row);

        Db::startTrans();//启动事务
        return $row;
    }
    /*------------------------------------------------------ */
    //-- 添加后调用
    /*------------------------------------------------------ */
    public function afterAdd($row)
    {


        Db::commit();// 提交事务

        return $this->success('添加成功', url('index'));
    }
    /*------------------------------------------------------ */
    //-- 修改前处理
    /*------------------------------------------------------ */
    public function beforeEdit($row)
    {
        if($row['tjnum']<1){
            return $this->error('推荐数量必须大于0', '');
        }
        if($row['times']<1){
            return $this->error('抽奖次数必须大于0', '');
        }
        $row['updatetime'] = time();

        Db::startTrans();//启动事务
        return $row;
    }
    /*------------------------------------------------------ */
    //-- 修改后调用
    /*------------------------------------------------------ */
    public function afterEdit($row)
    {
        return $this->success('修改成功', url('index'));
    }


}
